﻿using BCCommon;
using BCCommon.TMS.Insurance;
using BCDto.TMS.PersonalInsuranceRecord;
using BCService.TMS.Insurance.PersonalInsuranceRecord;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace BCTMSRESTful.Controllers.Management.TMS.Insurance.PersonalInsuranceRecord
{
    /// <summary>
    /// 个人保单记录
    /// </summary>
    [Route("api/v1/sim/[controller]")]
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    public class PersonalInsuranceRecordController : ManagementBaseController
    {
        private readonly IPersonalInsuranceRecordService personalInsuranceRecordService;
        private readonly IRedisService redisService;
        public PersonalInsuranceRecordController(IPersonalInsuranceRecordService personalInsuranceRecordService, IRedisService redisService)
        {
            this.personalInsuranceRecordService = personalInsuranceRecordService;
            this.redisService = redisService;
        }

        /// <summary>
        /// 创建个人保单记录
        /// </summary>
        /// <param name="dto"></param>
        /// <response code="200">创建个人保单记录成功。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("创建个人保单记录")]
        [ProducesResponseType(typeof(PersonalInsuranceRecordDto), 200)]
        public IActionResult Post([FromBody] PersonalInsuranceRecordRequestDto dto)
        {
            PersonalInsuranceRecordDto result = this.personalInsuranceRecordService.Add(this.Admin, dto);
            return this.Created(string.Format("/api/v1/sim/PersonalInsuranceRecord/{0}", result.PersonalInsuranceRecordId), result);
        }

        /// <summary>
        /// 获取指定的个人保单记录
        /// </summary>
        /// <param name="personalInsuranceRecordId">个人保单记录Id</param>
        /// <response code="200">查询个人保单记录成功。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("{personal_insurance_record_id}")]
        [ProducesResponseType(typeof(PersonalInsuranceRecordDto), 200)]
        public IActionResult Get([FromRoute(Name = "personal_insurance_record_id")] long personalInsuranceRecordId)
        {
            return Ok(this.personalInsuranceRecordService.GetByPersonalInsuranceRecordId(personalInsuranceRecordId));
        }

        /// <summary>
        /// 更新个人保单记录信息
        /// </summary>
        /// <param name="horsemanFreightExtentionId">个人保单记录Id</param>
        /// <param name="dto">个人保单记录修改内容</param>
        /// <response code="200">更新个人保单记录成功。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpPut("{personal_insurance_record_id}")]
        [Log("更新个人保单记录信息")]
        [ProducesResponseType(typeof(PersonalInsuranceRecordDto), 200)]
        public IActionResult Put([FromRoute(Name = "personal_insurance_record_id")] int horsemanFreightExtentionId, [FromBody] PersonalInsuranceRecordRequestDto dto)
        {
            return this.Ok(this.personalInsuranceRecordService.Update(horsemanFreightExtentionId, dto));
        }

        /// <summary>
        /// 获取个人保单记录（分页）
        /// </summary> 
        /// <param name="underwritingCompany">承保公司</param>
        /// <param name="insured">被保人</param>
        /// <param name="identityCardNo">身份证号</param>
        /// <param name="status">状态</param>
        /// <param name="index">分页页码</param>
        /// <param name="size">分页页数</param>
        /// <param name="ascending">是否升序排序</param>
        /// <param name="sortName">排序自动</param>
        /// <response code="200">获取个人保单记录列表成功。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<PersonalInsuranceRecordDto>), 200)]
        public IActionResult Get(
            [FromQuery(Name = "underwriting_company")] string underwritingCompany,
            [FromQuery(Name = "insured")] string insured,
            [FromQuery(Name = "identitycardno")] string identityCardNo,
            [FromQuery(Name = "insurance_status")] InsuranceStatus? status,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "sort_name")] string sortName,
            [FromQuery(Name = "index")] int index = 1,
            [FromQuery(Name = "size")] int size = 10)
        {
            if (this.Admin.IsSystemAccount)
            {
                return StatusCode((int)HttpStatusCode.OK, this.personalInsuranceRecordService.GetAll((long?)null, underwritingCompany, insured, identityCardNo, status, index, size, ascending, sortName));
            }
            return StatusCode((int)HttpStatusCode.OK, this.personalInsuranceRecordService.GetAll(this.Admin.AgentRecord == null ? this.Admin.CompanyId : this.Admin.AgentRecord.CompanyId, underwritingCompany, insured, identityCardNo, status, index, size, ascending, sortName));
        }

        /// <summary>
        /// 更新个人保单记录信息
        /// </summary>
        /// <param name="horsemanFreightExtentionId">个人保单记录Id</param>
        /// <response code="200">更新个人保单记录成功。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpDelete("{personal_insurance_record_id}")]
        [Log("删除个人保单记录信息")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult Delete([FromRoute(Name = "personal_insurance_record_id")] int horsemanFreightExtentionId)
        {
            return this.Ok(this.personalInsuranceRecordService.Delete(horsemanFreightExtentionId));
        }

    }
}
